package com.yanqu.road.server.manger.activity.bowart;

import com.yanqu.road.entity.activity.bowart.enums.eTargetType;

import java.math.BigDecimal;

public class TargetBehavior {

    static Coordinate bezierP1 = new Coordinate(80, 475);

    static Coordinate bezierP2 = new Coordinate(-560, -76);

    static Coordinate bezierP3 = new Coordinate(560, 76);

    static Coordinate bezierP4 = new Coordinate(-80, -475);

    /**
     * 距离
     */
    int distanceIdx;
    /**
     * 类型
     */
    int targetType;
    /**
     * 半径
     */
    double radius;

    public TargetBehavior(int distanceIdx, double radius, int targetType) {
        this.distanceIdx = distanceIdx;
        this.radius = radius;
        this.targetType = targetType;
    }

    public int getDistanceIdx() {
        return distanceIdx;
    }

    public void setDistanceIdx(int distanceIdx) {
        this.distanceIdx = distanceIdx;
    }

    public int getTargetType() {
        return targetType;
    }

    public void setTargetType(int targetType) {
        this.targetType = targetType;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    /**
     * 获取线性位移
     */
    private double getLineOffset(int gapTime, double cycleTime) {
        // 时间（毫秒） * 速度（像素每毫秒）
        double totalMove = BigDecimal.valueOf(gapTime).multiply(BigDecimal.valueOf(2 * radius)).divide(BigDecimal.valueOf(cycleTime), 5, BigDecimal.ROUND_UP).doubleValue();
        // 几倍距离
        int times = (int)Math.floor(totalMove / radius);
        int reducedTimes = times % 4;
        double offset = 0;
        double move = totalMove % radius;
        // 初始从中心向上（向右）
        if (reducedTimes == 0) {
            offset = move;
        } else if (reducedTimes == 1) {
            offset = radius - move;
        } else if (reducedTimes == 2) {
            offset = -move;
        } else if (reducedTimes == 3) {
            offset = -(radius - move);
        }
        return offset;
    }

    /**
     * 计算靶心坐标
     * @param gapTime
     * @param cycleTime  1周期时间（横杆/纵杆4倍半径距离的时间、圆一圈的时间、贝赛尔曲线一圈时间）
     * @return
     */
    public Coordinate getBullsEye2(int gapTime, int cycleTime){
        if(this.targetType == eTargetType.Fixed.getIntValue()){
            return new Coordinate(0, 0);
        }
        if(this.targetType == eTargetType.Transverse.getIntValue()){
            double x = getLineOffset(gapTime, cycleTime);
            return new Coordinate(x, 0);
        }
        if(this.targetType == eTargetType.Vertical.getIntValue()){
            double y = getLineOffset(gapTime, cycleTime);
            return new Coordinate(0, y);
        }
        if(this.targetType == eTargetType.Circular.getIntValue()){
            //圆心
            Coordinate center = new Coordinate(0, 0);
            //转速（每秒转动角度） * 转动时间（毫秒）
            double angle = BigDecimal.valueOf(360).multiply(BigDecimal.valueOf(gapTime)).divide(BigDecimal.valueOf(cycleTime),5, BigDecimal.ROUND_UP).doubleValue();
            //开始角度（90度），即从坐标（radius,0）开始逆时针移动
            int startAngle = 90;
            double radians = BigDecimal.valueOf(Math.toRadians(startAngle + angle)).setScale(5, BigDecimal.ROUND_UP).doubleValue();
            double x = center.x + BigDecimal.valueOf(radius).multiply(BigDecimal.valueOf(Math.cos(radians))).setScale(5, BigDecimal.ROUND_UP).doubleValue();
            double y = center.y + BigDecimal.valueOf(radius).multiply(BigDecimal.valueOf(Math.sin(radians))).setScale(5, BigDecimal.ROUND_UP).doubleValue();
            return  new Coordinate(x, y);
        }
        if(this.targetType == eTargetType.Serpentine.getIntValue()){
            Coordinate circleOneCenter = new Coordinate(140, 215);
            Coordinate circleTwoCenter = new Coordinate(-140, -215);
            //转过角度拆解
            double section = 133.2;//半圆弧长的角度
            double circleOneBegin = 103.68;//第1个圆开始角度
            double circleTwoBegin = 283.68;//第2个圆开始角度
            //转速（每秒转动角度） * 转动时间（毫秒）
            double angle = BigDecimal.valueOf(2 * section).multiply(BigDecimal.valueOf(gapTime)).
                    divide(BigDecimal.valueOf(cycleTime),5, BigDecimal.ROUND_UP).doubleValue();
            // 几倍角度
            int times = (int)Math.floor(angle / section);
            int reducedTimes = times % 4;
            double move = angle % section;
            double x = 0, y = 0;
            // 初始从中心向上
            if(reducedTimes == 0){
                double radians = BigDecimal.valueOf(Math.toRadians(circleOneBegin + section - move)).setScale(5, BigDecimal.ROUND_UP).doubleValue();
                x = circleOneCenter.x + BigDecimal.valueOf(radius).multiply(BigDecimal.valueOf(Math.cos(radians))).setScale(5, BigDecimal.ROUND_UP).doubleValue();
                y = circleOneCenter.y + BigDecimal.valueOf(radius).multiply(BigDecimal.valueOf(Math.sin(radians))).setScale(5, BigDecimal.ROUND_UP).doubleValue();
            }else if(reducedTimes == 1){
                double radians = BigDecimal.valueOf(Math.toRadians(circleOneBegin + move)).setScale(5, BigDecimal.ROUND_UP).doubleValue();
                x = circleOneCenter.x + BigDecimal.valueOf(radius).multiply(BigDecimal.valueOf(Math.cos(radians))).setScale(5, BigDecimal.ROUND_UP).doubleValue();
                y = circleOneCenter.y + BigDecimal.valueOf(radius).multiply(BigDecimal.valueOf(Math.sin(radians))).setScale(5, BigDecimal.ROUND_UP).doubleValue();
            }else if(reducedTimes == 2){
                double radians = BigDecimal.valueOf(Math.toRadians(circleTwoBegin + section - move)).setScale(5, BigDecimal.ROUND_UP).doubleValue();
                x = circleTwoCenter.x + BigDecimal.valueOf(radius).multiply(BigDecimal.valueOf(Math.cos(radians))).setScale(5, BigDecimal.ROUND_UP).doubleValue();
                y = circleTwoCenter.y + BigDecimal.valueOf(radius).multiply(BigDecimal.valueOf(Math.sin(radians))).setScale(5, BigDecimal.ROUND_UP).doubleValue();
            }else if(reducedTimes == 3){
                double radians = BigDecimal.valueOf(Math.toRadians(circleTwoBegin + move)).setScale(5, BigDecimal.ROUND_UP).doubleValue();
                x = circleTwoCenter.x + BigDecimal.valueOf(radius).multiply(BigDecimal.valueOf(Math.cos(radians))).setScale(5, BigDecimal.ROUND_UP).doubleValue();
                y = circleTwoCenter.y + BigDecimal.valueOf(radius).multiply(BigDecimal.valueOf(Math.sin(radians))).setScale(5, BigDecimal.ROUND_UP).doubleValue();
            }
            return  new Coordinate(x, y);
        }
        return null;
    }



}


